package com.gang.ssm.bos.system.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gang.ssm.bos.common.CriteriaStaff;
import com.gang.ssm.bos.common.Page;
import com.gang.ssm.bos.system.bean.Staff;
import com.gang.ssm.bos.system.bean.StaffExample;
import com.gang.ssm.bos.system.bean.StaffExample.Criteria;
import com.gang.ssm.bos.system.mapper.StaffMapper;
import com.gang.ssm.bos.system.service.StaffService;

@Service
public class StaffServiceImpl implements StaffService {

	@Autowired
	private StaffMapper staffMapper;
	
	@Override
	public List<Staff> findAll() {
		return staffMapper.selectByExample(null);
	}

	@Override
	public void pageQuery(Page<Staff> page, String name) {
		
		name = StringUtils.isBlank(name) ? "%%" : "%"+ name +"%";
		
		int total = (int) staffMapper.countByName(name);
		CriteriaStaff criteriaStaff = 
				new CriteriaStaff((page.getPageNo() - 1) * 
				page.getPageSize(), page.getPageSize(), name);
		List<Staff> staffs = staffMapper.findByCriteria(criteriaStaff);
		
		page.setTotalCount(total);
		page.setResult(staffs);
	}

	@Override
	public void save(Staff staff) {
		staffMapper.insert(staff);
	}

	@Override
	public void update(Staff staff) {
		staffMapper.updateByPrimaryKey(staff);
	}

	@Override
	public List<Staff> findListNoDelete() {
		StaffExample staffExample = new StaffExample();
		Criteria criteria = staffExample.createCriteria();
		criteria.andDeltagEqualTo("0");
		return staffMapper.selectByExample(staffExample);
	}

	@Override
	public void updateDelTagByStaffId(Staff staff) {
		staffMapper.updateByPrimaryKeySelective(staff);
	}

	@Override
	public void saveOrUpdate(Staff staff) {
		if(staff.getId() != null) {
			staffMapper.updateByPrimaryKeySelective(staff);
		}else{
			staffMapper.insertSelective(staff);
		}
			
	}

	@Override
	public Staff findByStaffId(Integer staffId) {
		return staffMapper.selectByPrimaryKey(staffId);
	}

}
